package com.seckill.job.task;

import com.seckill.common.core.domain.R;
import com.seckill.common.redis.service.RedisService;
import com.seckill.job.domain.TbSeckill;
import com.seckill.job.domain.TbStock;
import com.seckill.job.service.Feign_secKill;
import com.seckill.system.api.RemoteGoodsService;
import com.seckill.system.api.RemoteMiaoShaService;
import com.seckill.system.api.RemoteMsgService;
import com.seckill.system.api.domain.Seckill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import com.seckill.common.core.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Autowired
    private RemoteGoodsService remoteGoodsService;
    @Autowired
    Feign_secKill feign_secKill;
    @Autowired
    RedisTemplate redisTemplate;




    @Autowired
    private RemoteMiaoShaService remoteMiaoShaService;

    @Autowired
    private RedisService redisService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    /*public void secKillTask() {
        List<TbSeckill> notOn = feign_secKill.getNotOn();

        for (TbSeckill secKill : notOn) {
            LocalDateTime now = LocalDateTime.now();
            if (now.plusMinutes(30).compareTo(secKill.getSeckillCreatedate()) > 0) {
                TbStock stock = feign_secKill.findbyskuid(secKill.getSkuId());
                redisTemplate.opsForValue().set("miao" + secKill.getSkuId(), stock.getStock());

            }
        }
    }  */

        //将30 分钟执行内开始秒杀的商品放入redis
    public void timedAddSpike()
    {
        //查询秒杀数据
        List<Seckill> listStatus = remoteGoodsService.getListStatus(2);
        for (int i = 0; i <listStatus.size() ; i++) {
            Seckill seckill = listStatus.get(i);
            long time = seckill.getSeckillCreatedate().getTime();
            long time1 = seckill.getSeckillEnddate().getTime();
            long now = new Date().getTime()+1800000;//1800000 是30分钟的毫秒值
            if (now >= time) {
                System.out.println("1");
                //将信息存入redis
                redisService.setCacheObject(seckill.getSkuId()+"",seckill.getSeckillStock());
                redisService.setCacheObject("goods"+seckill.getSkuId(),seckill);
                //修改状态
                remoteGoodsService.updateSeckillStatus(1,seckill.getSkuId());
                //静态页面生成
                remoteMiaoShaService.sendStaticPage(seckill.getSkuId());
            }
        }
        System.out.println("timedAddSpike");
    }

    //秒杀库存缓存redis
    public void  cacheMiaoshaNum()
    {
        //查询秒杀数据
        List<Seckill> listStatus = remoteGoodsService.getListStatus(1);
        for (int i = 0; i <listStatus.size() ; i++) {
            Seckill seckill = listStatus.get(i);
            redisService.setCacheObject(seckill.getSkuId()+"",seckill.getSeckillStock());
        }
        //remoteMiaoShaService.sendStaticPage(2l);
        System.out.println("秒杀库存缓存redis");
    }

}
